using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Xmobile.Users;
using Portal.Core.DAL;

namespace Portal.GUI.Administrator.SyncCategory
{
    public partial class SyncCategory : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Xmobile.Users.MainDB objdbUser = new Xmobile.Users.MainDB();

            // Update vao bang User_Category
            /*using (Xmobile.Users.MainDB objdb = new Xmobile.Users.MainDB())
            {
                DataTable dtUser_Category = objdb.SelectQuery("Select * from User_Category");
                Xmobile.Users.CategoryRow catRow;
                Portal.Core.DAL.CategoryRow catRowChannelCore;
                
                foreach (DataRow dr in dtUser_Category.Rows)
                {
                    catRow = objdb.CategoryCollection.GetByPrimaryKey(Convert.ToInt32(dr["Cat_ID"].ToString()));
                    catRowChannelCore = objdbCoreDal.CategoryCollection.GetByPrimaryKey(Convert.ToInt32(dr["Cat_ID"].ToString()));
                    if (catRow.Cat_Name.Trim().ToLower() != catRowChannelCore.Cat_Name.Trim().ToLower())
                    {
                        objdb.SelectQuery("Update User_Category Set Cat_ID = " + catRowChannelCore.Cat_ID + " Where UC_ID = " + dr["UC_ID"]);
                    }
                   
                }
            }*/

            // Delete Old Category
            using (Xmobile.Users.MainDB objdb = new Xmobile.Users.MainDB())
            {
                objdb.SelectQuery(" Delete Category Where Channel_ID = " + Portal.API.Config.CurrentChannel);
            }


            // Update vao bang Category
            using (Portal.Core.DAL.MainDB objdb = new Portal.Core.DAL.MainDB())
            {
                DataTable dtCategory = objdb.SelectQuery("Select * from Category");
                Xmobile.Users.CategoryRow catRowUser;
                foreach (DataRow dr in dtCategory.Rows)
                {
                    catRowUser = objdbUser.CategoryCollection.GetByPrimaryKey(Convert.ToInt32(dr["Cat_ID"].ToString()), Portal.API.Config.CurrentChannel);
                    if (catRowUser == null)
                    {

                        if (dr["Cat_ParentID"].ToString() != "0")
                        {
                            objdbUser.SelectQuery("Insert Into Category(Cat_ID,Channel_ID,Cat_Name,Cat_Parent,Cat_Order)" +
                                                "Values('" + dr["Cat_ID"].ToString() + "'," +
                                                        "'" + Portal.API.Config.CurrentChannel + "', " +
                                                        "N'" + dr["Cat_Name"] + "'," +
                                                        "'" + dr["Cat_ParentID"] + "'," +
                                                        "'" + dr["Cat_Order"] + "'" + ")");
                        }
                        else
                        {
                            
                            objdbUser.SelectQuery("Insert Into Category(Cat_ID,Channel_ID,Cat_Name,Cat_Order)" +
                                                "Values('" + dr["Cat_ID"].ToString() + "'," +
                                                        "'" + Portal.API.Config.CurrentChannel + "', " +
                                                        "N'" + dr["Cat_Name"] + "'," +
                                                        "'" + dr["Cat_Order"] + "'" + ")");
                        }


                        
                    }
                    
                }
            }

        }
    }
}